package com.orhon.smartcampus.modules.base.mutation;

import com.alibaba.fastjson.JSON;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.GradesDao;
import com.orhon.smartcampus.modules.base.dao.PeriodsDao;
import com.orhon.smartcampus.modules.base.dao.SubjectsDao;
import com.orhon.smartcampus.modules.base.entity.Grades;
import com.orhon.smartcampus.modules.base.entity.Periods;
import com.orhon.smartcampus.modules.base.entity.Subjects;
import com.orhon.smartcampus.modules.base.gqlInputModel.GQLSubjectInput;
import com.orhon.smartcampus.modules.base.inputMapper.SubjectInputMapper;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class AddSubjectMutation implements DataFetcher {


    @Autowired
    SubjectsDao subjectsDao;

    @Autowired
    GradesDao gradesDao;

    @Autowired
    PeriodsDao periodsDao;

    /**
     * 新增/修改学科信息
     * editor : ths
     */
    @Override
    @Transactional
    public Object get(DataFetchingEnvironment environment) throws Exception {
        if (environment.getArgument("inputData") != null) {
            GQLSubjectInput input = SubjectInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

//            System.out.println("input-----");
//            System.out.println(input);

            Subjects subjects = null;
            if (input.getId() != null) {
                subjects = subjectsDao.findById(input.getId());
            } else {
                subjects = new Subjects();
                subjects.setCreatedAt(new Date());
            }
            if(input.getSubject_number() != null){

                subjects.setSubjectNumber(input.getSubject_number());
            }
            if(input.getSubject_slug() != null){

                subjects.setSubjectSlug(input.getSubject_slug());
            }
            if(input.getMark() != null){

                subjects.setMark(input.getMark());
            }
            if(input.getSubject_order() != null){

                subjects.setSubjectOrder(input.getSubject_order());
            }
            if(input.getType() != null){

                subjects.setType(input.getType());
            }
            if(input.getSubject_name() != null){

                subjects.setSubjectName(JSON.toJSONString(input.getSubject_name()));
            }
            if (input.getGrade() != null) {
                try {
                    List<Grades> gradesList = new ArrayList<>();
                    input.getGrade().forEach(g -> {
                        Grades grade = gradesDao.get(Grades.class, Long.valueOf(g.intValue()));
                        gradesList.add(grade);
                    });
                    subjects.setGrade(gradesList);
                } catch (Exception e) {
                    return Result.error(500, "" + e);
                }
            }
            if (input.getPeriod() != null) {
                try {
                    List<Periods> periodsList = new ArrayList<>();
                    input.getPeriod().forEach(p -> {
                        Periods periods = periodsDao.get(Periods.class, Long.valueOf(p.intValue()));
                        periodsList.add(periods);
                    });
                    subjects.setPeriod(periodsList);
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.error(500, "" + e);
                }
            }
            try {
                subjectsDao.saveOrUpdate(subjects);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error(500, "" + e);
            }

            return Result.ok();

        }
        return Result.error();
    }
}

